import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), vueJsx()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
   server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8001',
        secure: false,
        changeOrigin: true,
        configure: (proxy, options) => {
        proxy.on('proxyReq', (proxyReq, req) => {
          // 强制修改请求头中的 Host 为后端地址
          proxyReq.setHeader('Host', 'localhost:8001');
          // 打印请求路径（调试用）
          console.log('代理请求路径:', req.url);
        });
      }
        // rewrite: (path) => path.replace(/^\/api/, '')
        // rewrite: (path) => {
        //   const newPath = path.replace(/^\/api/, '');
        //   console.log(`路径重写: ${path} -> ${newPath}`);
        //   return newPath;
        // },
      //    configure: (proxy, options) => {
      //   proxy.on('proxyReq', (proxyReq, req, res) => {
      //     console.log('正在代理请求:', req.url);
      //   });
      //   proxy.on('proxyRes', (proxyRes, req, res) => {
      //     console.log('代理响应:', proxyRes.statusCode, req.url);
      //   });
      // }
      }
    }
  }
})
